package com.lzs.chuku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzs.chuku.mapper.OutorderDetailMapper;
import com.lzs.chuku.mapper.PickDetailMapper;
import com.lzs.chuku.mapper.PicktaskMapper;
import com.lzs.chuku.service.OutorderService;
import com.lzs.entity.OutorderDetail;
import com.lzs.entity.Pick;
import com.lzs.chuku.mapper.PickMapper;
import com.lzs.chuku.service.PickService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzs.entity.PickDetail;
import com.lzs.entity.Picktask;
import com.lzs.util.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘中帅
 * @since 2020-11-23
 */
@Service
public class PickServiceImpl extends ServiceImpl<PickMapper, Pick> implements PickService {

    @Autowired
    private OutorderService outorderService;
    @Autowired
    private OutorderDetailMapper outorderDetailMapper;
    @Autowired
    private PicktaskMapper picktaskMapper;
    @Autowired
    private PickDetailMapper pickDetailMapper;
    @Autowired
    private PickMapper pickMapper;

    @Override
    public R findPickState(Pick pick, Integer currentPage, Integer pageSIize) {
        try{
            String pickno=pick.getPickno();
            String picktaskno=pick.getPicktaskno();
            String state = pick.getState();
            QueryWrapper<Pick> wrapper=new QueryWrapper<>();
            if (StringUtils.isNotEmpty(pickno)){
                wrapper.like("pickno",pickno);
            }
            if (StringUtils.isNotEmpty(picktaskno)){
                wrapper.like("picktaskno",picktaskno);
            }
            if (StringUtils.isNotEmpty(state)){
                wrapper.eq("state",state);
            }
            Page<Pick> page=new Page<>(currentPage,pageSIize);
            pickMapper.selectPage(page,wrapper);
            return R.ok().message("查询成功").data("page",page);
        }catch (RuntimeException e){
            return R.error().message("查询列表失败");
        }

    }

    @Override
    public R changeState(Long pickId, String s) {
        try {
            pickMapper.changeState(pickId, s);
            return R.ok().message("成功");
        }catch (RuntimeException e){
            return R.error().message("结束失败");
        }
    }

    /**
     * 判断是否录入完成，数量和出口路订单表中不一致 则直接返回失败，
     * 注意：不需要判断两个明细种商品是否相同 ，因为在录入时已经判断 保证一定商品一致；
     * @param pick
     * @return
     */
    @Override
    public R overPick(Pick pick) {
        Pick pick1 = pickMapper.selectById(pick.getId());
        QueryWrapper<PickDetail> wrapper=new QueryWrapper<>();
        wrapper.eq("pid",pick.getId());
        List<PickDetail> pickDetails = pickDetailMapper.selectList(wrapper);
        Picktask picktask = picktaskMapper.selectById(pick1.getPicktaskid());
        QueryWrapper<OutorderDetail> wrapper1=new QueryWrapper<>();
        wrapper1.eq("pid",picktask.getOutorderid());
        List<OutorderDetail> outorderDetails = outorderDetailMapper.selectList(wrapper1);
        if (pickDetails.size()!=outorderDetails.size()){
            return R.error().message("还未分拣完毕，不可结束！！！");
        }else{
            boolean flag=false;
            for (int i = 0; i <pickDetails.size() ; i++) {
                for (int j = 0; j < outorderDetails.size(); j++) {
                    if (pickDetails.get(i).getCommodityid().equals(outorderDetails.get(j).getCommodityid())){
                        if(!pickDetails.get(i).getQty().equals(outorderDetails.get(j).getQty())){
                            flag=true;
                            break;
                        }
                    }
                }
            }
            if (flag){
                return R.error().message("还未分拣完毕，不可结束！！！");
            }
            changeState(pick.getId(),"2");
            List list=new ArrayList();
            list.add(picktask.getOutorderid());
            outorderService.changestate(list,"5");
            return R.ok().message("成功");
        }
    }
}
